package cn.favebook.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;

import cn.favebook.Global;
import cn.favebook.bean.User;
import cn.favebook.db.UserDao;

/**
 * Servlet implementation class indexSignServlet
 */
@WebServlet("/indexSignServlet")
public class indexSignServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
       
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF8");
		String vcode = request.getParameter("vcode");
		
		String expected = (String) request.getSession().getAttribute(Global.VCODE_KEY);
		if(!vcode.equals(expected)) {
			response.sendRedirect("./index.jsp?status=vcodeerror");
			return;
		}
		
		

		
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		
		QueryRunner runner = (QueryRunner) request.getServletContext().getAttribute("runner");
		User user = new User(username, password, email);
		try {
			UserDao.register(runner, user);
		} catch (Exception e) {
			e.printStackTrace();
			response.sendError(500);
			return;
		}
		
		response.sendRedirect("./signin.jsp");
	}

}
